
# Replication code for Figure 3b (and Figure A.2) in Jenke & Sullivan, "Attention and Political Choice: A Foundation for Eye Tracking in Political Science," Political Analysis.
# Written by Libby Jenke

rm(list = ls())

library(ggplot2)
library(dplyr)
devtools::install_github("libbyjenke/PlotEyeScan")
library(PlotEyeScan)

# Insert working directory below
setwd("....")

# Load data ---------------------------------------------------------------
eye_dat <- read.csv("data/sample_data.csv")

# Get data set ---------------------------------------------------------------
# Rename variables and define parameters
fix_data <- eye_dat |>
  rename(
    subjID = Respondent.Name,
    aoi_num = AOI.Label,
    fixDuration = Fixation.Duration,
    fixX = `Fixation.X`,
    fixY = `Fixation.Y`,
    trial_num = trial_num
  )

# Define trial number(s) that you want to examine
trial_number <- 1

# Get rid of any 0s for AOI numbers
fix_data <- fix_data[!is.na(fix_data$aoi_num),]

#Define additional parameters
image_path <- "data/stimulus_screen.png"
eyetracker_width <- 1920
eyetracker_height <- 1080
trial_num <- 1
output_path <- "output/{subjID}_trial_{trial_num}_scan.png"
overlap_threshold <- 50

#Use the PlotEyeScan function (Jenke and Sullivan, 2025) (output will be sent to "output" folder)
PlotEyeScan(fix_data, image_path, eyetracker_width, eyetracker_height, trial_num, output_path, overlap_threshold)

